﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RrslLite.Net.Messages;
using RrslLite.Net.Proxies;

namespace App.Test.RrslLiteTEST
{
    // does not work yet ....

    [TestClass]
    public class AsyncDuplexMessageProcessorProxyTEST
    {
        private static AsyncDuplexMessageProcessorProxy GetAsyncDuplexMessageProcessorProxy()
        {
            EndpointAddress address = new EndpointAddress("http://localhost/App.Host.Web/WcfDuplexMessageService.svc");
            PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding();

            binding.CloseTimeout = new TimeSpan(0, 10, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);

            return new AsyncDuplexMessageProcessorProxy(binding, address);
        }

        [TestMethod]
        public void AsyncDuplexMessageProcessorProxy_cTor()
        {
            var testee = GetAsyncDuplexMessageProcessorProxy();

            Assert.IsNotNull(testee);
        }

        [TestMethod]
        public void AsyncDuplexMessageProcessorProxy_InvokeOperationsAsync()
        {
            ManualResetEvent manualResetEvent = new ManualResetEvent(false);

            var testee = GetAsyncDuplexMessageProcessorProxy();

            var messages = new Message[]
                               {
                                   new Message(Guid.NewGuid())
                                       {
                                           OperationContainerName = "IPersonFacade",
                                           OperationName = "GetActivePersonsFor",
                                           OperationParameters = new Dictionary<string,object>() { { "country", "CZ" } }
                                       },
                                   new Message(Guid.NewGuid())
                                       {
                                           OperationContainerName = "ICountryFacade",
                                           OperationName = "GetByCode",
                                           OperationParameters = new Dictionary<string,object>() { { "country", "CZ" } }
                                       }
                               };

            testee.ReceiveReceived +=
            (s, e) =>
            {
                Assert.IsNotNull(e.Result);
                Assert.IsTrue(e.Result.Length > 0);

                manualResetEvent.Set();
            };

            testee.InvokeOperationsAsync(messages);

            manualResetEvent.WaitOne();
        }
    }
}
